package com.example.siyun.controller.productcontroller;

import com.example.siyun.pojo.*;
import com.example.siyun.service.productservice.JsProductService;
import com.example.siyun.util.JsonResult;
import com.example.siyun.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
 * 商品列表
 * @author：刘宝龙
 *@Date: 2022-10-12
 * */
@Slf4j
@RestController
@RequestMapping("/JsProduct")
public class ProductController {

    @Autowired
    private JsProductService jsProductService;


    /**
     * 表的展示
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/advanced/getAll")
    public PageResult getPage(int page,int limit){
        PageHelper.startPage(page,limit);
        //查询所有数据
        List<JsProduct> all = jsProductService.getAll();
        //注意，不要忘了传
        PageInfo<JsProduct> info = new PageInfo<>(all);

        PageResult jsonResult = new PageResult();
        jsonResult.setMessage("成功");
        jsonResult.setState(0);
        jsonResult.setData(info.getList());
        jsonResult.setTotal(info.getTotal());
        return jsonResult;
    }


    /**
     * 表的添加
     * @param jsProduct
     * @return
     */
    @RequestMapping("/advanced/add")
    public JsonResult add(JsProduct jsProduct){
        Map<String,String> map = new HashMap<>();
        System.out.println(jsProduct);
        JsonResult js = new JsonResult();
        log.info("添加信息");
        try{
            jsProductService.insert(jsProduct);
            map.put("id",jsProduct.getProductId()+"");
            map.put("name",jsProduct.getProductName());
            js.setState(200);
            js.setMessage("成功");
            js.setData(map);
        }catch (Exception e){
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("添加完成");
        return js;
    }


    /**
     * 表的删除
     * @param productId
     * @return
     */
    @RequestMapping("/advanced/delete")
    public JsonResult delete(Integer productId) {

        System.out.println(productId);
        JsonResult js = new JsonResult();
        log.info("删除信息");
        try {
            jsProductService.delete(productId);
            js.setState(200);
            js.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("删除信息成功");
        return js;
    }

    /**
     * 表的修改
     * @param jsProduct
     * @return
     */
    @RequestMapping("/advanced/update")
    public JsonResult update(JsProduct jsProduct) {

        System.out.println(jsProduct);
        JsonResult js = new JsonResult();
        log.info("修改信息");
        try {
            jsProductService.update(jsProduct);
            js.setState(200);
            js.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("修改信息成功");
        return js;
    }

    /**
     * 表的开关修改
     * @param productId
     * @param state
     * @return
     */
    @RequestMapping("/advanced/state")
    public JsonResult state(int productId,int state){
        System.out.println(productId);
        System.out.println(state);
        JsonResult js = new JsonResult();
        log.info("修改信息");
        try{
            jsProductService.state(productId,state);
            js.setState(200);
            js.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setState(400);
            js.setMessage("添加失败");
        }
        log.info("修改信息成功");
        return js;
    }

    /**
     * 全查日志
     * @param page
     * @param limit
     * @param productId
     * @return
     */
    @RequestMapping("/advanced/getrizhi")
    public PageResult rizhi(Integer page, Integer limit, int productId) {
        System.out.println("-----------------------");
        System.out.println(productId);
        System.out.println("-----------------------");
        PageHelper.startPage(page, limit);
        List<JsProduct> list = jsProductService.riZhi(productId);
        PageInfo<JsProduct> pageInfo = new PageInfo<>(list);

        PageResult jsonResult = new PageResult();
        jsonResult.setMessage("成功");
        jsonResult.setState(0);
        jsonResult.setData(pageInfo.getList());
        jsonResult.setTotal(pageInfo.getTotal());
        jsonResult.getTotal(pageInfo.getTotal());
        return jsonResult;

    }

    /**
     * 审核详情
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @RequestMapping("/advanced/shxq")
    public PageResult shxq(int page, int limit,int id) {
        System.out.println(id);
        PageHelper.startPage(page, limit);
        List<Pshenhe> list = jsProductService.shxq(id);
        System.out.println(list);
        PageInfo<Pshenhe> pageInfo = new PageInfo<>(list);
        PageResult jsonResult = new PageResult();

        jsonResult.setMessage("成功");
        jsonResult.setState(0);
        jsonResult.setData(pageInfo.getList());
        jsonResult.setTotal(pageInfo.getTotal());
        jsonResult.getTotal(pageInfo.getTotal());
        return jsonResult;
    }

    /**
     * 审核按钮
     * @param pshenhe
     * @param session
     * @return
     */
    @RequestMapping("/advanced/shan")
    public Map<String, Object> addcourseprocess(Pshenhe pshenhe, HttpSession session){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        pshenhe.setPtime(time);
        String auditname = (String) session.getAttribute("loginname");
        pshenhe.setPrenyuan(auditname);
        System.out.println(pshenhe);
        Map<String, Object> map = new HashMap<>();
        Map aa=new HashMap();
        log.info("修改信息");
        if(pshenhe.getPjieguo().equals("审核通过")){
            int ps=1;
            jsProductService.shenHeAnNiu(pshenhe);
            jsProductService.psh(ps,pshenhe.getPid());
        } else if (pshenhe.getPjieguo().equals("审核不通过")) {
            int ps=2;
            jsProductService.psh(ps,pshenhe.getPid());
            jsProductService.shenHeAnNiu(pshenhe);
        }
        log.info("修改信息成功");
        map.put("status",200);
        return map;
    }

    /**
     * 商品日志
     * @param page
     * @param limit
     * @param
     * @return
     */
    @RequestMapping("/advanced/loginlog")
    public PageResult loginlog(Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        List<Productlog> list = jsProductService.log();
        PageInfo<Productlog> pageInfo = new PageInfo<>(list);
        PageResult jsonResult = new PageResult();
        jsonResult.setMessage("成功");
        jsonResult.setState(0);
        jsonResult.setData(pageInfo.getList());
        jsonResult.setTotal(pageInfo.getTotal());
        jsonResult.getTotal(pageInfo.getTotal());
        return jsonResult;
    }

    /**
     * 全查操作日志
     * @param page
     * @param limit
     * @param productcaozuo
     * @return
     */
    @RequestMapping("/advanced/gettablelog")
    public PageResult getAll(int page, int limit,Productcaozuo productcaozuo) {
        PageHelper.startPage(page, limit);
        List<Productcaozuo> list = jsProductService.find(productcaozuo);
        PageInfo<Productcaozuo> pageInfo = new PageInfo<>(list);
        PageResult jsonResult = new PageResult();
        jsonResult.setMessage("成功");
        jsonResult.setState(0);
        jsonResult.setData(pageInfo.getList());
        jsonResult.setTotal(pageInfo.getTotal());
        jsonResult.getTotal(pageInfo.getTotal());
        return jsonResult;
    }

    /**
     * 删除操作日志
     * @param id
     * @return
     */
    @RequestMapping("/advanced/delrizhi")
    public PageResult del(int id){
        log.info("删除信息");
        jsProductService.delRiZhi(id);
        PageResult jsonResult = new PageResult();
        jsonResult.setState(200);
        log.info("删除信息成功");
        return jsonResult;
    }

    /**
     * 添加操作日志
     * @param productcaozuo
     * @param session
     * @return
     */
    @RequestMapping("/advanced/addrizhi")
    public PageResult addoperatelog(Productcaozuo productcaozuo, HttpSession session){
        log.info("添加信息");
        Timestamp time = new Timestamp(System.currentTimeMillis());
        productcaozuo.setOperatetime(time);
        String name= (String) session.getAttribute("loginname");
        productcaozuo.setName(name);
        jsProductService.addRiZhi(productcaozuo);
        PageResult jsonResult = new PageResult();
        jsonResult.setState(200);
        log.info("添加商品日志信息成功");
        return jsonResult;
    }
}
